//
//  HHConversationNoNetCell.m
//  HHChatKit
//
//  Created by Henry on 2021/8/10.
//

#import "HHConversationNoNetCell.h"
#import <HHTool/HHTool.h>
#import "HHResources.h"
#import "HHConversationModel.h"

@interface HHConversationNoNetCell ()

@property (nonatomic, strong) UILabel *titleLabel;

@property (nonatomic, strong) UIImageView *iconView;

@end

@implementation HHConversationNoNetCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self p_initViews];
    }
    return self;
}

- (void)setupFont {
    _titleLabel.font = self.model.config.noNetTextFont;
}

- (void)p_initViews {
    _iconView = [[UIImageView alloc] init];
    _iconView.image = [HHResources getImageFromMessage:@"msg_error"];

    _titleLabel = [[UILabel alloc] init];
    _titleLabel.text = GetHCLocalizableString(@"NetworkUnavailable");

    [self.contentView addSubview:_iconView];
    [self.contentView addSubview:_titleLabel];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.iconView.mm_left(20).mm_width(20).mm_height(20).mm__centerY(self.contentView.mm_centerY);
    self.titleLabel.mm_sizeToFit().mm_left(self.iconView.mm_maxX+20).mm_flexToRight(10).mm__centerY(self.contentView.mm_centerY);
}

- (void)setModel:(HHConversationModel *)model {
    _model = model;
    self.backgroundColor = model.config.noNetColor;
    _titleLabel.textColor = model.config.noNetTextColor;

    if (model.name.length > 0) {
        _titleLabel.text = model.name;
    } else {
        _titleLabel.text = GetHCLocalizableString(@"NetworkUnavailable");
    }
    [self setupFont];
}

+ (NSString *)identifier {
    return NSStringFromClass([HHConversationNoNetCell class]);
}

+ (void)registerCellToTableView:(UITableView *)tableView {
    [tableView registerClass:[HHConversationNoNetCell class] forCellReuseIdentifier:[HHConversationNoNetCell identifier]];
}

+ (instancetype)dequeueOrCreateCellByTableView :(UITableView *)tableView {
    HHConversationNoNetCell *cell = [tableView dequeueReusableCellWithIdentifier:[HHConversationNoNetCell identifier]];
    if (cell == nil) {
        cell = [[HHConversationNoNetCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[[self class] identifier]];
    }
    return cell;
}

+ (CGFloat)viewHeight:(HHConversationModel *)model {
    return model.config.nameFont.lineHeight + model.config.margin + model.config.margin;
}

@end
